<?php
if(isset($_POST['action']))
{
    $db = new Database;
    // User Info
        $auth = Auth::getAuth('current_user');
        $user_id = $auth['id'];
        $company_id =  $auth['company_id'];
        $user_group = $auth['role'];
        $user_groupType = $auth['role_type'];
        $userPositionID = $auth['position_id'];
        $getPosition = $db->query("SELECT * FROM tbposition where id = '$userPositionID'","row");
        
    $page = $_POST['page'];
    $cur_page = $page;
    $page -= 1;
    $per_page = 20; // Per page
    $previous_btn = true;
    $next_btn = true;
    $first_btn = true;
    $last_btn = true;
    $start = $page * $per_page;
    $action = $_POST['action'];
    
    // For Sorting
    /*
        $dataSortValue = $_POST['dataSortValue'];
        if(!empty($dataSortValue)){
            if($_POST['counterSort']%2==0){
                $sort = "ORDER BY date DESC";
            }else{
                $sort = "ORDER BY date ASC";
            }
        }else{
            $sort = "ORDER BY form_title";
        }
    */
    if($action=="FloatingAcc")
    {
        $queryRequest = $db->query("SELECT * FROM tbuser where status = 1 and company_id = '$company_id' and is_active = 1 and position_id!=0 $sort LIMIT $start, $per_page","array");
        $count = $db->query("SELECT * FROM tbuser where status = 1 and company_id = '$company_id' and is_active = 1 and position_id!=0","numrows");
        
        $no_of_paginations = ceil($count / $per_page);
    }elseif($_POST['action']=="search_request"){
        $value = $_POST['value'];
        $count = $db->query("SELECT * FROM tbrequest r
                                    LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                    LEFT JOIN tbform f on f.id=r.form_id
                                    LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                                    LEFT JOIN tbset_field tbset_field on tbset_field.companyID=f.company_id
                                    WHERE (".roleApprover($user_groupType,$user_group)."
                                    or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                    AND rp.status=0 AND r.status=0 AND rp.status=0 and r.status=0 and r.company_id = $company_id
                                    AND f.form_title LIKE '%$value%'
                                    
                                    OR (r.user_id='".getUserID($value)."')
                                    AND (".roleApprover($user_groupType,$user_group)."
                                    or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                    AND rp.status=0 AND r.status=0 AND rp.status=0 and r.status=0 and r.company_id = $company_id
                                    
                                    ","numrows");
        
        $queryRequest = $db->query("SELECT sf.numCol as numCol, sf.colValue_formID as colValue_formID,
                                   r.date as date_request,r.user_id as user_id, f.id as form_id,
                                rp.wf_master_id as wf_master_id, f.form_title as form_title, r.data as data,
                                r.date as dateRequest,
                                r.id as request_id,rp.id as requestProcess_id FROM tbrequest r
                                LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                LEFT JOIN tbform f on f.id=r.form_id
                                LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                                
                                WHERE
                                
                                (".roleApprover($user_groupType,$user_group)."
                                or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."')
                                AND rp.status=0 and r.status=0 AND f.form_title LIKE '%$value%'
                                
                                OR (r.user_id='".getUserID($value)."')
                                AND (".roleApprover($user_groupType,$user_group)."
                                or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                AND rp.status=0 and r.status=0 and r.company_id = $company_id
                                
                                LIMIT $start, $per_page","array");
        $no_of_paginations = ceil($count / $per_page);
    }elseif($_POST['action']=="searchDate"){
        $value = $_POST['value'];
        $split = explode(",", $value);
        $count = $db->query("SELECT * FROM tbrequest r
                                    LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                    LEFT JOIN tbform f on f.id=r.form_id
                                    LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                                    WHERE (".roleApprover($user_groupType,$user_group)."
                                    or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                    and rp.status=0 and r.company_id = $company_id
                                    and r.status=0 AND r.date>='$split[0]' AND r.date<='$split[1]'","numrows");
        
        
        $queryRequest = $db->query("SELECT sf.numCol as numCol, sf.colValue_formID as colValue_formID,
                                   r.date as date_request,r.user_id as user_id, f.id as form_id,
                                rp.wf_master_id as wf_master_id, f.form_title as form_title, r.data as data,
                                r.id as request_id,rp.id as requestProcess_id FROM tbrequest r
                                LEFT JOIN tbrequest_process rp on r.id=rp.request_id
                                LEFT JOIN tbform f on f.id=r.form_id
                                LEFT JOIN tbset_field sf on sf.companyID=f.company_id
                                WHERE  (".roleApprover($user_groupType,$user_group)."
                                or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."')
                                AND rp.status=0 and r.status=0 AND r.date>='$split[0]' AND r.date<='$split[1]' and r.company_id = $company_id LIMIT $start, $per_page","array");
        $no_of_paginations = ceil($count / $per_page);
    }
    $page = $page+1;
    $finaldata.= '<table class="table_data">
                        <thead>
                            <tr id="sortRequestApprove" data-location="tblRequestToApprove" data-search="form_title" data-tag-action="RequestApprove" p="'.$page.'" data-element="#requestToApprove_tbl">
                                <th>Name</th>
                                <th>Floating Group</th>
                                <th colspan="2">Action</th>';
    $finaldata.= '          </tr>
                        </thead>
                        <tbody class="listUserBody">';
    
    if($count>0){
        foreach($queryRequest as $dataRequest){
            $getActiveRole = $db->query("SELECT * from tborgchart o LEFT JOIN tborgchartobjects oo on o.id=oo.orgChart_id where o.status=1 and o.company_id = $company_id and oo.role='".$dataRequest['role']."'","row");
            
            if($dataRequest['role']!=$getActiveRole['role']){
            $finaldata.= ' 
                <tr id="userFloatList_'.$dataRequest['id'].'">
                    <td style="text-align:center">'.setName($dataRequest['firstname'],$dataRequest['middlename'],$dataRequest['lastname']).'</td>
                    <td style="text-align:center">
                    <span class="spanRole" id="spanRole_'.$dataRequest['id'].'">'.$dataRequest['role'].'</span>';
                    
                    $userRole = $dataRequest['role'];
                    $activate = $db->query("SELECT * from tborgchart where status = '1' and company_id = '$company_id'","row");
                    $diagram_id = $activate['id'];
                    $selectRole = $db->query("SELECT role from tborgchartobjects where orgChart_id = '$diagram_id'","array");
                $finaldata.= '     
                    <select class="input-xlarge userRoleEdit" id="userRoleEdit_'.$dataRequest['id'].'" style="display: none">';
                        
                        foreach($selectRole as $dataRole){
                        $finaldata.= '    
                            <option '.setSelected($userRole,$dataRole['role']).'  value="'.$dataRole['role'].'">'.$dataRole['role'].'</option>
                        ';    
                        }
                        foreach($selectTableRole as $dataTableRole){
                        $finaldata.= ' 
                            <option '.setSelected($userRole,$dataTableRole['role']).' value="'.$dataTableRole['role'].'">'.$dataTableRole['role'].'</option>
                        ';   
                        }
                $finaldata.= '         
                    </select>
                    </td>
                    <td style="text-align:center">
                        <a class="icon-edit actionUser editFloatingUser" rel="editFloatingUser" style="cursor: pointer" id="editFloatingUser_'.$dataRequest['id'].'" title="Update Account"></a>
                        <a class="icon-ok actionUser saveEditFloatingUser"  rel="saveEditFloatingUser" style="cursor: pointer;display: none;margin-right: 5px" id="saveEditFloatingUser_'.$dataRequest['id'].'" title="Save Account"></a>
                        <a class="icon-remove actionUser cancelFloatingEditUser" rel="cancelEditUser" style="cursor: pointer;display: none" id="cancelEditUser_'.$dataRequest['id'].'" title="Cancel Update"></a>
                    </td>
                    <td style="text-align:center"><a class="icon-trash actionUser" rel="deactivateUser" style="cursor: pointer" id="'.$dataRequest['id'].'" title="Deactivate"></a></td>
                </tr>';
            }
	}
        //pagination
        pagination($cur_page,$page,$per_page,$start,$previous_btn,$next_btn,$first_btn,$last_btn,$no_of_paginations,$finaldata,$count,$action,$value);
    }else{
        $finaldata.='<tr><td colspan="3"><center>No Record Found</center></td></tr>';
        echo $finaldata;
    }
    
}
?>
<script>
    $(".actionUserRequest").tooltip();
</script>
